package servlet;

import bean.News;
import bean.NewsofUser;
import bean.User;
import dao.NewsDao;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebServlet("/newsMan.do")
public class NewsManServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public NewsManServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("UTF-8");
//        response.setCharacterEncoding("UTF-8");
        String updateid = request.getParameter("updateid");//修改
        String deleteid = request.getParameter("deleteid");//删除
        String select ="";
        select += request.getParameter("select");//删除

        System.out.println("updateid="+updateid);
        System.out.println("deleteid="+deleteid);
        System.out.println("select="+select);
        if(updateid!=null){
            System.out.println("修改新闻暂未完善");
            News news = NewsDao.Update(Integer.parseInt(updateid));


//            String newsContent = news.getContent();
//            System.out.println("newsContent="+newsContent);
//            request.getSession().setAttribute("newsContent", newsContent);
//// 正则表达式匹配URL
//            String urlRegex = "(https?://[\\w\\-\\.]+(:\\d+)?(/([\\w/_\\-\\.]*[^\\.,;\"\'<\\s])?)?)";
//            Pattern urlPattern = Pattern.compile(urlRegex);
//            Matcher urlMatcher = urlPattern.matcher(newsContent);
//
//            StringBuilder result = new StringBuilder();
//            int lastIndex = 0;
//
//            while (urlMatcher.find()) {
//                // 在URL前面的内容去除标签
//                String textBeforeUrl = newsContent.substring(lastIndex, urlMatcher.start()).replaceAll("<[^>]*>", "");
//
//                result.append(textBeforeUrl);
//                // 添加原始URL
//                result.append(urlMatcher.group());
//                lastIndex = urlMatcher.end();
//            }
//
//// 处理最后一个URL之后的内容
//            result.append(newsContent.substring(lastIndex).replaceAll("<[^>]*>", ""));
//
//            String finalContent = result.toString();
//
//            news.setContent(finalContent);
//



//            request.setAttribute("news", news);
//            request.getRequestDispatcher("./jsp/publish.jsp").forward(request, response);
            System.out.println("news="+news);
            request.getSession().setAttribute("news", news);
            response.sendRedirect("./jsp/publish.jsp");
            return;
        }
        if(deleteid!=null){
            NewsDao.Delete(Integer.parseInt(deleteid));
            List<NewsofUser> list =  NewsDao.select();
            //request.setAttribute("list", list);
            request.getSession().setAttribute("list", list);
            response.sendRedirect("/press_release_system_war_exploded/testlimit");
//            response.sendRedirect("./jsp/News.jsp");
            return;
        }
        if(!select.isEmpty()){

            List<NewsofUser> list =  NewsDao.select(select);
            //request.setAttribute("list", list);
            request.getSession().setAttribute("list", list);
//            response.sendRedirect("/press_release_system_war_exploded/testlimit");
            response.sendRedirect("/press_release_system_war_exploded/newsselectlimit"); // Redirect to welcome page

//            response.sendRedirect("./jsp/News.jsp");
//            System.out.println("没有select");
            System.out.println(select+"111");
            return;
        }
        response.sendRedirect("/press_release_system_war_exploded/testlimit"); // Redirect to welcome page

//        response.sendRedirect("./jsp/News.jsp");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
